package csa_test

import (
	"git.sr.ht/~mil/mobroute/src/csa"
	"git.sr.ht/~mil/mobroute/src/db"
	"git.sr.ht/~mil/mobroute/src/logger"
	"testing"
)

type CheapestArrivalDestinationTestCaseArgs struct {
	StopToArrivalDetails map[string]csa.ArrivalDetails
	StopWalkcosts        map[string]uint
	MaxWalkcost          uint
}

type CheapestArrivalDestinationTestCase struct {
	Test   string
	Args   CheapestArrivalDestinationTestCaseArgs
	Expect string
}

func Test_CheapestArrivalDestination(t *testing.T) {
	logger.LogWarn = true

	var (
		testCases = []CheapestArrivalDestinationTestCase{
			{
				Test: "Works",
				Args: CheapestArrivalDestinationTestCaseArgs{
					StopToArrivalDetails: map[string]csa.ArrivalDetails{
						"a": csa.ArrivalDetails{Time: 7, Connection: &db.Connection{}},
						"b": csa.ArrivalDetails{Time: 2, Connection: &db.Connection{}},
						"c": csa.ArrivalDetails{Time: 5, Connection: &db.Connection{}},
						"d": csa.ArrivalDetails{Time: 8, Connection: &db.Connection{}},
					},
					StopWalkcosts: map[string]uint{"a": 1, "b": 2, "c": 3, "d": 4},
					MaxWalkcost:   99,
				},
				Expect: "b",
			},
			{
				Test: "Works",
				Args: CheapestArrivalDestinationTestCaseArgs{
					StopToArrivalDetails: map[string]csa.ArrivalDetails{
						"a": csa.ArrivalDetails{Time: 7, Connection: &db.Connection{}},
						"b": csa.ArrivalDetails{Time: 6, Connection: &db.Connection{}},
						"c": csa.ArrivalDetails{Time: 3, Connection: &db.Connection{}},
						"d": csa.ArrivalDetails{Time: 8, Connection: &db.Connection{}},
					},
					StopWalkcosts: map[string]uint{"a": 3, "b": 9, "c": 6, "d": 4},
					MaxWalkcost:   99,
				},
				Expect: "c",
			},
			// TODO: unit test to ignore entries where connection is nil
		}
	)

	for _, testCase := range testCases {
		if result, err := csa.CheapestArrivalDestination(
			testCase.Args.StopToArrivalDetails,
			testCase.Args.StopWalkcosts,
			testCase.Args.MaxWalkcost,
		); err != nil {
			t.Fatalf("CheapestArrivalDestination error: %v", err)
		} else if result != testCase.Expect {
			t.Fatalf("Want %v; got %v", testCase.Expect, result)
		}
	}
}
